<?php
namespace app\common\validate\tysj;

class Product extends Base {
    /**
     * 验证规则
     */
    protected $rule = [
        'lock'                 => 'require',
        'id'                   => 'require',
        'sid'                  => 'require|isUser:tysj\\Store',
        'photo'                => 'require|integer|isUser:tysj\\Image',
        'photos'               => 'require|photos',
        'name'                 => 'require|max:32|xunique:tysj\\Product,id,sid',
        'selling'              => 'require|max:100',
        'specs'                => 'require|specs',
        'cid'                  => 'require|integer|isOptions:ProductClassify',
        'osid'                 => 'require|integer|isOptions:OstSale',
        'info_photo'           => 'require|photos',
        'to'                   => 'require|isOptions:ProductExamine',
        'examine_after_status' => 'isOptions:ProductStatus',
        'areaid'               => 'isOptions:NativeProductArea'
    ];

    /**
     * 提示消息
     */
    protected $message = [
        'lock.require'                => '验证规则场景不存在',
        'id.require'                  => '产品ID不能为空',
        'id.isEdit'                   => '该产品不属于您',
        'id.isCompleted'              => '产品等待审核,不能上线',
        'id.isLook'                   => '产品暂未上线',
        'id.isBuy'                    => '产品暂未上线',
        'sid.require'                 => '店铺ID不能为空',
        'sid.isUser'                  => '该店铺不属于您',
        'photo.require'               => '产品主图不能为空',
        'photo.integer'               => '产品主图ID不合法',
        'photo.isUser'                => '产品主图提交的图片不属于您',
        'photos.photos'               => '产品图片',
        'specs.require'               => '规格不能为空',
        'specs.specs'                 => '规格',
        'name.require'                => '产品名称不能为空',
        'name.max'                    => '产品名称超过32个字符',
        'name.xunique'                => '产品名称已存在',
        'selling.require'             => '产品卖点不能为空',
        'selling.max'                 => '产品卖点不能超过100个字符',
        'cid.require'                 => '产品类型ID不能为空',
        'cid.integer'                 => '产品类型ID不合法',
        'cid.isOptions'               => '产品类型ID不合法',
        'osid.require'                => '产品售后类型ID不能为空',
        'osid.integer'                => '产品售后类型ID不合法',
        'osid.isOptions'              => '产品售后类型ID不合法',
        'info_photo.require'          => '产品详情图不能为空',
        'info_photo.photos'           => '产品详情图',
        'areaid.isOptions'            => '土特产区域不存在'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'save'        => [
            'id' => 'isOwner:tysj\\Product',
            'sid',
            'photo',
            'photos',
            'name',
            'selling',
            'specs',
            'cid',
            'osid',
            'info_photo',
            'examine_after_status',
            'areaid'
        ],
        'private'      => ['sid'],
        'look'         => ['id' => 'require|isBuy'],
        'look_all'     => ['id'],
        'info'         => ['id' => 'require|isEdit'],
        'preview'      => ['id' => 'require|isEdit'],
        'status'       => ['id' => 'require|isEdit'],
        'show'         => ['id' => 'require|isEdit|isCompleted:tysj\Product'],
        'del'          => ['id' => 'require|isEdit'],
        'buy'          => ['id' => 'isBuy']
    ];

    //是否可以购买
    protected function isBuy($value, $rule, $data = [], $name) {
        $productEnum = loadEnum('ProductStatus');
        $storeEnum = loadEnum('StoreStatus');
        $product = loadModel('tysj\\Product')->where(['id' => $value])->find();
        if(empty($product)) {
            return false;
        }
        $store = loadModel('tysj\\Store')->where(['id' => $product['sid']])->find();
        return $productEnum->isShow($product['status']) && $storeEnum->isShow($store['status']);
    }

    //产品图片列表
    protected function photos($value, $rule, $data = [], $name) {
        $msg = $this->message[$name . '.photos'];
        if(!is_array($value)) {
            return $msg . '不合法';
        }
        $imageValidate = loadValidate('tysj\\Image');
        foreach ($value as $k => $v) {
            if(!$imageValidate->scene('nexus')->check(['id' => $v, 'uid' => $data['uid']])) {
                return $msg . $v . '错误，'. $imageValidate->getError();
                break;
            }
        }
        return true;
    }

    //规格列表
    protected function specs($value, $rule, $data = [], $name) {
        $msg = $this->message[$name . '.specs'];
        if(!is_array($value)) {
            return $msg . '不合法';
        }

        $productSpecsValidate = loadValidate('tysj\\ProductSpecs');
        foreach ($value as $k => $v) {
            if(!$productSpecsValidate->scene('used')->check(['id' => $v, 'uid' => $data['uid']])) {
                return $msg . $v . '错误，'. $productSpecsValidate->getError();
                break;
            }
        }
        return true;
    }

    //产品是否通过审核
    protected function isCompleted($value, $rule, $data = []) {
        $model = loadModel($rule);
        $enum = loadEnum('ProductExamine');
        $data = $model->find($value);
        return $data && $data['examine'] == $enum->complete();
    }

    //是否具有产品的编辑权限
    protected function isEdit($value, $rule, $data = []) {
        $model = loadModel('tysj\\Product');
        $product = $model->find($value);
        if(empty($product)) {
            return false;
        }
        $storeModel = loadModel('tysj\\Store');
        $store = $storeModel->find($product['sid']);
        return $store && $store['uid'] == $data['uid'];
    }
}
